import md5 from 'md5'
//签名密钥---------前后端一致，如有血药请自行修改
const signatureSecret = "dd05f1c54d63749eda95f9fa6d49v442a";

export default class sginMd5Utils{


    static getDateTimeToString() {
        const date_ = new Date()
        const year = date_.getFullYear()
        let month = date_.getMonth() + 1
        let day = date_.getDate()
        if (month < 10) month = '0' + month
        if (day < 10) day = '0' + day
        let hours = date_.getHours()
        let mins = date_.getMinutes()
        let secs = date_.getSeconds()
        const msecs = date_.getMilliseconds()
        if (hours < 10) hours = '0' + hours
        if (mins < 10) mins = '0' + mins
        if (secs < 10) secs = '0' + secs
        if (msecs < 10) secs = '0' + msecs
        return year + '' + month + '' + day + '' + hours + '' + mins + '' + secs
    }
    /**
     *
     * @param url 请求的url
     * @returns{{}} 将url请求参数组成json对象(url?后面的参数)
     */
    static parseQueryString(url){
        let urlReg = /^[^\?]+\?([\w\W]+)$/,
            paramReg = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
            //exec:检索正则
            urlArray = urlReg.exec(url),
            result = {}

        //截取url后面的含有参数部分
        // console.log(typeof(url))
        let lastpathVariable = url.substring(url.lastIndexOf("/")+1);
        //判断是含有参数
        if(lastpathVariable.includes(",")){
            if(lastpathVariable.includes("?")){
                //含有参数，就截取参数
                lastpathVariable=lastpathVariable.substring(0,lastpathVariable.indexOf("?"))
            }
            //没搞懂！！
            result["x-path-variable"] = decodeURIComponent(lastpathVariable);

        }
        if (urlArray && urlArray[1]) {
            let paramString = urlArray[1], paramResult;
            while ((paramResult = paramReg.exec(paramString)) != null) {
                //数字值转为string类型，前后端加密规则保持一致
                if(this.myIsNaN(paramResult[2])){
                    paramResult[2] = paramResult[2].toString()
                }
                result[paramResult[1]] = paramResult[2];
            }
        }
        return result;


    }


    /**
     *
     * @param objectOne
     * @param objectTwo
     * @returns {*}  将两个对象合并
     */
    static mergeObject(objectOne, objectTwo) {
        if (objectTwo && Object.keys(objectTwo).length > 0) {
            for (let key in objectTwo) {
                if (objectTwo.hasOwnProperty(key) === true) {
                    //数字值转为string类型，前后端加密规则保持一致
                    if(this.myIsNaN(objectTwo[key])){
                        objectTwo[key] = objectTwo[key].toString()
                    }
                    objectOne[key] = objectTwo[key];
                }
            }
        }
        return objectOne;
    }

    /**
     * json参数升序
     * @param jsonObj 发送参数
     */

    static sortAsc(jsonObj) {
        let arr = new Array();
        let num = 0;
        for (let i in jsonObj) {
            arr[num] = i;
            num++;
        }
        let sortArr = arr.sort();
        let sortObj = {};
        for (let i in sortArr) {
            sortObj[sortArr[i]] = jsonObj[sortArr[i]];
        }
        return sortObj;
    }



    static getSign(url,requestParams){
        let urlParams = this.parseQueryString(url)
        let jsonObj = this.mergeObject(urlParams,requestParams)
        let requestBody = this.sortAsc(jsonObj);
        return md5(JSON.stringify(requestBody)+signatureSecret).toUpperCase();

    }
    static myIsNaN(value) {
        return typeof value === 'number' && !isNaN(value);
    }













}
